home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993…ch: Other People's Memory / ADC Developer CD (1993-03) (''Other People's Memory'')_iso / Dev.CD Mar 93.iso / Technical Documentation / Sample Code / DTS.Lib & Samples / DTS.Draw / ToolPalette.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-22  |  8.4 KB  |  350 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        ToolPalette.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* This file contains the code for the document procedure pointers for the DTS.Draw
  12. ** tool palette document.  The tool palette document does very little, so most of
  13. ** the document procedure pointers are set to nil.  Imaging and clicking are the
  14. ** only two methods that we need to support. */
  15.  
  16.  
  17.  
  18. /*****************************************************************************/
  19.  
  20.  
  21.  
  22. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  23. #include "App.Common.h"        /* Get the stuff in common with rez.            */
  24. #include "App.protos.h"        /* Get the prototypes for the application.        */
  25.  
  26. #ifndef __AppMenu__
  27. #include "App.Menu.h"
  28. #endif
  29.  
  30. #ifndef __ERRORS__
  31. #include <Errors.h>
  32. #endif
  33.  
  34. #ifndef __FONTS__
  35. #include <Fonts.h>
  36. #endif
  37.  
  38. #ifndef __RESOURCES__
  39. #include <Resources.h>
  40. #endif
  41.  
  42. #ifndef __TOOLUTILS__
  43. #include <ToolUtils.h>
  44. #endif
  45.  
  46. #ifndef __TREEOBJ2__
  47. #include "TreeObj2.h"
  48. #endif
  49.  
  50. #ifndef __UTILITIES__
  51. #include "Utilities.h"
  52. #endif
  53.  
  54.  
  55. /*****************************************************************************/
  56.  
  57.  
  58.  
  59. short        gTool;
  60. Boolean        gKeepTool;
  61. WindowPtr    gToolWindow;
  62.  
  63. static void            ToolContentClick(WindowPtr window, EventRecord *event, Boolean firstClick);
  64. static void            DrawIcon(Rect *rct, short iconID);
  65. static WindowPtr    GetToolWindow(short id, Ptr storage, Boolean vis, WindowPtr relWindow,
  66.                                   WindowPtr behind, Boolean inColor, Rect sizeInfo, long refCon);
  67. static Rect            PlaceToolWindow(WindowPtr window, WindowPtr relatedWindow, Rect sizeInfo);
  68.  
  69.  
  70.  
  71. /*****************************************************************************/
  72. /*****************************************************************************/
  73.  
  74.  
  75.  
  76. /* Open an invisible tool palette.  The user will have to tear off the menu before
  77. ** a tool palette is visible. */
  78.  
  79. #pragma segment ToolPalette
  80. OSErr    OpenToolPalette(void)
  81. {
  82.     OSErr        err;
  83.     FileRecHndl    frHndl;
  84.  
  85.     err = NewDocument(&frHndl, kToolFileType, false);
  86.     if (!err) {
  87.         err = DoNewWindow(frHndl, &gToolWindow, FrontWindow(), (WindowPtr)-1);
  88.         if (err)
  89.             DisposeDocument(frHndl);
  90.     }
  91.  
  92.     if (err)
  93.         HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  94.     else
  95.         SetPaletteTool(0, false);
  96.  
  97.     return(err);
  98. }
  99.  
  100.  
  101.  
  102. /*****************************************************************************/
  103.  
  104.  
  105.  
  106. /* Just imaging and clicking are handles by the tool palette. */
  107.  
  108. #pragma segment ToolPalette
  109. OSErr    ToolInitDocument(FileRecHndl frHndl)
  110. {
  111.     FileRecPtr    frPtr;
  112.  
  113.     frPtr = *frHndl;
  114.     frPtr->fileState.windowID                = rToolWindow;
  115.     frPtr->fileState.getDocWindow            = GetToolWindow;
  116.     frPtr->fileState.calcFrameRgnProc        = nil;
  117.     frPtr->fileState.contentClickProc        = ToolContentClick;
  118.     frPtr->fileState.contentKeyProc          = nil;
  119.     frPtr->fileState.drawFrameProc           = nil;
  120.     frPtr->fileState.freeDocumentProc        = nil;
  121.     frPtr->fileState.freeWindowProc          = nil;
  122.     frPtr->fileState.imageProc               = ToolImageDocument;
  123.     frPtr->fileState.initContentProc         = nil;
  124.     frPtr->fileState.readDocumentProc        = nil;
  125.     frPtr->fileState.readDocumentHeaderProc  = nil;
  126.     frPtr->fileState.resizeContentProc       = nil;
  127.     frPtr->fileState.scrollFrameProc         = nil;
  128.     frPtr->fileState.undoFixupProc           = nil;
  129.     frPtr->fileState.windowCursorProc        = nil;
  130.     frPtr->fileState.writeDocumentProc       = nil;
  131.     frPtr->fileState.writeDocumentHeaderProc = nil;
  132.  
  133.     frPtr->fileState.fss.name[0] = 0;    /* Use resource window name. */
  134.     frPtr->fileState.attributes  = kwToolWindow;
  135.  
  136.     return(noErr);
  137. }
  138.  
  139.  
  140.  
  141. /*****************************************************************************/
  142. /*****************************************************************************/
  143.  
  144.  
  145.  
  146. /* Find out which tool was clicked or double-clicked on. */
  147.  
  148. #pragma segment ToolPalette
  149. static void    ToolContentClick(WindowPtr window, EventRecord *event, Boolean firstClick)
  150. {
  151. #pragma unused (firstClick)
  152.  
  153.     WindowPtr    oldPort;
  154.     Point        pt;
  155.     short        tool;
  156.     long        tick;
  157.     static long    lastClick;
  158.  
  159.     SetPort(window);                    /* We are supposed to be here. */
  160.  
  161.     GetPort(&oldPort);
  162.     SetPort(window);
  163.     pt = event->where;
  164.     GlobalToLocal(&pt);
  165.  
  166.     tool = pt.v / kToolHeight;
  167.     tick = TickCount();
  168.  
  169.     if (tool < kNumTools) {
  170.         gKeepTool = false;
  171.         if (tick < (lastClick + 20))
  172.             if ((tool) && (tool == gTool))
  173.                 gKeepTool = true;
  174.         gTool     = tool;
  175.         lastClick = tick;
  176.         SetPaletteTool(gTool, gKeepTool);
  177.     }
  178.  
  179.     SetPort(oldPort);
  180. }
  181.  
  182.  
  183.  
  184. /*****************************************************************************/
  185.  
  186.  
  187.  
  188. /* Draw the tool palette icons into the port.  If DTS.LIB..framework calls us, the
  189. ** port is already set, but we set it here so that we can call this function
  190. ** directly.  Calling it directly is desireable for certain operations, such
  191. ** as when the user clicks with a one-shot tool.  When this occurs, we revert
  192. ** back to the arrow tool.  Also, if a tool is permanently selected and the
  193. ** user clicks in a document but then doesn't grow out an object, we also revert
  194. ** to the arrow tool.  Another case is if the user selects a tool via the menu. */
  195.  
  196. #pragma segment ToolPalette
  197. OSErr    ToolImageDocument(FileRecHndl frHndl)
  198. {
  199. #pragma unused (frHndl)
  200.  
  201.     WindowPtr    oldPort;
  202.     short        i, j;
  203.     Rect        iconRect;
  204.  
  205.     GetPort(&oldPort);
  206.     SetPort(gToolWindow);
  207.  
  208.     for (i = 0; i < kNumTools; ++i) {        /* Draw an icon for each tool. */
  209.         iconRect.left   = 0;
  210.         iconRect.top    = i * kToolHeight;
  211.         iconRect.right  = 32;
  212.         iconRect.bottom = iconRect.top + kToolHeight - 1;
  213.         if ((j = i) == gTool) {
  214.             j += 50;
  215.             if (gKeepTool)
  216.                 j += 50;
  217.         }
  218.         DrawIcon(&iconRect, j + rArrowIcon);
  219.         MoveTo(0, iconRect.top + kToolHeight - 1);
  220.         Line(32, 0);
  221.     }
  222.  
  223.     SetPort(oldPort);
  224.     return(noErr);
  225. }
  226.  
  227.  
  228.  
  229. /*****************************************************************************/
  230. /*****************************************************************************/
  231.  
  232.  
  233.  
  234. #pragma segment ToolPalette
  235. static void    DrawIcon(Rect *rct, short iconID)
  236. {
  237.     Handle        iconHndl;
  238.     Rect        rct32;
  239.     RgnHandle    oldClip, newClip;
  240.  
  241.     if (iconHndl = GetResource('ICN#', iconID)) {
  242.         GetClip(oldClip = NewRgn());
  243.         newClip = NewRgn();
  244.         RectRgn(newClip, rct);
  245.         SetClip(newClip);
  246.         rct32.right  = (rct32.left = rct->left) + 32;
  247.         rct32.bottom = (rct32.top  = rct->top ) + 32;
  248.         PlotIcon(&rct32, iconHndl);
  249.         SetClip(oldClip);
  250.         DisposeRgn(oldClip);
  251.         DisposeRgn(newClip);
  252.     }
  253. }
  254.  
  255.  
  256.  
  257. /*****************************************************************************/
  258.  
  259.  
  260.  
  261. /* This code is used if the application is modified to start with a visible tool
  262. ** palette.  There didn't used to be a tool menu, and the palette was simply
  263. ** displayed upon boot time.  This code positions the window in the upper-right
  264. ** of the main screen. */
  265.  
  266. #pragma segment ToolPalette
  267. WindowPtr    GetToolWindow(short id, Ptr storage, Boolean vis, WindowPtr relWindow,
  268.                             WindowPtr behind, Boolean inColor, Rect sizeInfo, long refCon)
  269. {
  270.     return(GetSomeKindOfWindow(PlaceToolWindow, id, storage, vis, relWindow,
  271.                                behind, inColor, sizeInfo, refCon));
  272. }
  273.  
  274.  
  275.  
  276. /*****************************************************************************/
  277.  
  278.  
  279.  
  280. /* Proc for the function GetToolWindow(). */
  281.  
  282. #pragma segment ToolPalette
  283. Rect    PlaceToolWindow(WindowPtr window, WindowPtr relatedWindow, Rect sizeInfo)
  284. {
  285. #pragma unused (relatedWindow, sizeInfo)
  286.  
  287.     Rect    scnRct, cntRct;
  288.     short    dx, dy;
  289.  
  290.     scnRct = GetMainScreenRect();
  291.     cntRct = GetWindowContentRect(window);
  292.  
  293.     dx = cntRct.right  - cntRct.left;
  294.     dy = cntRct.bottom - cntRct.top;
  295.  
  296.     cntRct.right  = scnRct.right - 5;
  297.     cntRct.top    = scnRct.top   + 33;
  298.     cntRct.left   = cntRct.right - dx;
  299.     cntRct.bottom = cntRct.top + dy;
  300.  
  301.     MoveWindow(window, cntRct.left, cntRct.top, false);
  302.     return(cntRct);
  303. }
  304.  
  305.  
  306.  
  307. /*****************************************************************************/
  308.  
  309.  
  310.  
  311. /* Tool palette display housekeeping function. */
  312.  
  313. #pragma segment ToolPalette
  314. void    ToolPaletteDisplay(void)
  315. {
  316.     if (((WindowPeek)gToolWindow)->visible)
  317.         HideWindow(gToolWindow);
  318.     else {
  319.         ShowHide(gToolWindow, true);
  320.         BringToFront(gToolWindow);
  321.         HiliteWindows();
  322.     }
  323. }
  324.  
  325.  
  326.  
  327. /*****************************************************************************/
  328.  
  329.  
  330.  
  331. /* Tool palette set tool function. */
  332.  
  333. #pragma segment ToolPalette
  334. void    SetPaletteTool(short tool, Boolean keepTool)
  335. {
  336.     MenuHandle    menu;
  337.     short        i;
  338.  
  339.     gTool     = tool;
  340.     gKeepTool = keepTool;
  341.     ToolImageDocument(nil);
  342.  
  343.     menu = GetMHandle(mToolPalette);
  344.     for (i = iArrowTool; i <= iPieTool; ++i) SetItemMark(menu, i, noMark);
  345.     SetItemMark(menu, gTool + 1, '1' + keepTool);
  346. }
  347.  
  348.  
  349.  
  350.